/**
 * Copyright (c) 2024-2025 National Fundamental Software of China Co., Ltd.
 *
 * You should have find a copy of NFSChina License. If not, please contact us
 * by email <os_support@nfschina>
 *
**/

import QtQuick 2.15
import QtQml 2.15
import QtQuick.Layouts 1.15
import QtQuick.Templates 2.15 as T
import QtGraphicalEffects 1.15
import QtQuick.Controls 2.5
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.components 3.0 as PC3
import org.kde.plasma.extras 2.0 as PlasmaExtras
import org.kde.kirigami 2.13 as Kirigami
import org.kde.kcoreaddons 1.0 as KCoreAddons
import org.kde.kquickcontrolsaddons 2.0 as KQuickAddons

PC3.ToolBar {
    id: root

    property alias searchText: searchField.text
    property real preferredNameAndIconWidth: 0
    property real scaleFactor: PlasmaCore.Units.devicePixelRatio;

    contentHeight: searchField.implicitHeight
    contentWidth: searchField.implicitWidth
    leftPadding: 0
    rightPadding: 0

    background: Rectangle {
        anchors.fill: parent
        opacity: 0
        border.color: "red"
        border.width: 1 * scaleFactor
    }

    spacing: plasmoid.rootItem.backgroundMetrics.spacing

    RowLayout {
        id: rowLayout
        spacing: root.spacing
        height: parent.height
        anchors {
            left: parent.left
            right: parent.right
        }
        Keys.onDownPressed: plasmoid.rootItem.contentArea.forceActiveFocus(Qt.TabFocusReason)
        PlasmaExtras.SearchField {
            id: searchField
            Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
            Layout.fillWidth: true
            Layout.rightMargin: 10 * scaleFactor
            implicitHeight: 36 * scaleFactor
            implicitWidth: 298 * scaleFactor
            iconLeftMargin: 18 * scaleFactor
            background: Rectangle{
                radius: 8 * scaleFactor
                color: "#0E0F0F"
                opacity: 0.1
            }

            focus: true
            Binding {
                target: plasmoid.rootItem
                property: "searchField"
                value: searchField
                // there's only one header ever, so don't waste resources
                restoreMode: Binding.RestoreNone
            }
            Connections {
                target: plasmoid
                function onExpandedChanged() {
                    if (plasmoid.expanded) {
                        searchField.clear()
                    }
                }
            }
            onTextEdited: {
                searchField.forceActiveFocus(Qt.ShortcutFocusReason)
            }
            onAccepted: {
                plasmoid.rootItem.contentArea.currentItem.action.triggered()
                plasmoid.rootItem.contentArea.currentItem.forceActiveFocus(Qt.ShortcutFocusReason)
            }
            Keys.priority: Keys.AfterItem
            Keys.forwardTo: plasmoid.rootItem.contentArea !== null ? plasmoid.rootItem.contentArea.view : []
            Keys.onLeftPressed: if (activeFocus) {
                                    if (LayoutMirroring.enabled) {
                                        nextItemInFocusChain().forceActiveFocus(Qt.TabFocusReason)
                                    } else {
                                        nextItemInFocusChain(false).forceActiveFocus(Qt.BacktabFocusReason)
                                    }
                                }
            Keys.onRightPressed: if (activeFocus) {
                                     if (!LayoutMirroring.enabled) {
                                         nextItemInFocusChain().forceActiveFocus(Qt.TabFocusReason)
                                     } else {
                                         nextItemInFocusChain(false).forceActiveFocus(Qt.BacktabFocusReason)
                                     }
                                 }
        }

        Rectangle {
            id: switch_fullScreen
            Layout.alignment: Qt.AlignRight | Qt.AlignVCenter
            visible: true
            width: 36 * scaleFactor
            height: width
            radius: 8
            anchors {
                right: parent.right
            }
            color:"transparent"
            Kirigami.Icon {
                id : watermarkIcon
                anchors {
                    verticalCenter: parent.verticalCenter
                    horizontalCenter: parent.horizontalCenter
                }
                width: 16 * scaleFactor
                height: width
                source: "launcher_fullScreen"
            }
            readonly property string tipContent: i18n("all applications")
            ToolTip {
                id: allTip
                delay: Kirigami.Units.toolTipDelay
            }
            MouseArea {
                anchors.fill: parent
                hoverEnabled: true
                onClicked: {
                    plasmoid.expanded = false
                    containmentInterface.createApplet(plasmoid ,"org.kde.plasma.kicker")
                }
                onEntered: {
                    switch_fullScreen.color = Qt.rgba(255, 255, 255, 0.5)
                    allTip.show(switch_fullScreen.tipContent, 2000)
                }

                onExited: {
                    switch_fullScreen.color = "transparent"
                    allTip.hide()
                }
            }
        }
    }
}
